<?php

class User extends Model
{
    const USER_TABLE_NAME = 'mbuser';

    public static function checkLogin($params)
    {
        $user = new User();

        $params = array_merge(array('encrypted' => false, 'use_id' => false), $params);

        $params['encrypted'] || $params['pass'] = App::password($params['pass']);

        if ($params['use_id']) {
            $condition = 'u_id = :user';
        } else {
            $condition = 'u_taikhoan = :user';
        }

        $params = App::filterArray($params, array('user', 'pass'));


        $rows = $user->Db->select(User::USER_TABLE_NAME, $condition . ' AND u_matkhau = :pass', $params);

        return count($rows) > 0;
    }

    public static function changePassword($u_id, $pass, $use_username = false)
    {
        $use_username && $u_id = self::getUserIdByUsername($u_id);
        $db = new Database();
        $db->run('UPDATE mbuser SET u_matkhau = :mk WHERE u_id = :id', array(
            'id' => $u_id,
            'mk' =>  App::password($pass)
        ));
    }

    public static function getUserType($uid)
    {
        $user = new User();

        return $user->Db->executeScalar('SELECT g_id FROM ' . User::USER_TABLE_NAME . ' WHERE u_id = :id',
            array('id' => $uid)) == 1 ? 'admin' : 'user';
    }

    public static function getUserInfo($uid, $use_username = false)
    {
        $user = new User();
        # Nếu sử dụng username thì chuyển thành ID
        if ($use_username) {
            $uid = self::getUserIdByUsername($uid);
        }
        $info = $user->Db->select(User::USER_TABLE_NAME, 'u_id = :id', array('id' => $uid));
        return count($info) ? $info[0] : FALSE;
    }


    public static function getUserIdByUsername($username)
    {
        $db = new Database();
        return $db->executeScalar('SELECT u_id FROM ' . self::USER_TABLE_NAME . ' WHERE u_taikhoan = :username',
            array('username' => $username));
    }

    public static function exists($username)
    {
        return self::getUserIdByUsername($username) != false;
    }

    public static function register($group, $user, $pass, $name, $address, $phone, $email = '')
    {
        $group == 'admin' && $group = 1;
        $group == 'user' && $group = 2;
        is_numeric($group) || $group = 1;

        if (self::exists($user)) {
            return false;
        }
        $db = new Database();
        if ($db->run('INSERT INTO ' . self::USER_TABLE_NAME .
            '(g_id, u_ten, u_diachi, u_sodt, u_email, u_taikhoan, u_matkhau) ' .
            ' VALUES(:g, :ten, :dc, :dt, :em, :tk, :mk)', array(
            'g' => $group,
            'ten' => $name,
            'dc' => $address,
            'dt' => $phone,
            'em' => $email,
            'tk' => $user,
            'mk' => App::password($pass)
        )) == 1) {
            return true;
        }

        return false;
    }

    public static function update($u_id, $u_ten, $u_diachi, $u_sodt, $u_email)
    {
        self::$_database->update(self::USER_TABLE_NAME,
            compact('u_ten', 'u_diachi', 'u_sodt', 'u_email'),
            'u_id = :id', array('id' => $u_id));
    }
}
